package chen.web.forum;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import chen.web.DataSourceProvider;
import chen.web.user.Account;
import chen.web.user.AccountManager;
import chen.web.util.DateInt;

public class ForumAccountLoaderImpl implements ForumAccountLoader {

	private static SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(DataSourceProvider.getDataSource());
	private AccountManager am;
	public ForumAccountLoaderImpl(AccountManager am){
		this.am = am;
	}

	private static final String SQL_1 = "select * from web_account_forum where acc_id = ?"; 
	@Override
	public ForumAccount loadForumAccount(int id) {
		// TODO Auto-generated method stub
		Account acc = am.getAccount(id);
		if(acc == null)	return null;
		ForumAccount fa = null;
		try{
			fa = simpleJdbcTemplate.queryForObject(SQL_1, new ForumAccountRowMapper(acc), id);	
		} catch(EmptyResultDataAccessException e){
			fa = new ForumAccount(acc);
			initForumAccount(fa);
		}
		return fa;
	}

	@Override
	public List<ForumAccount> loadForumAccount(List<Integer> ids) {
		// TODO Auto-generated method stub
		List<Account> accList = am.getAccount(ids);
		if(accList.size() == 0) return new ArrayList<ForumAccount>(0);

		//构造sql
		StringBuilder sb = new StringBuilder();
		sb.append("select * from web_account_forum where acc_id in(");
		for(int i=0; i<ids.size(); i++){
			sb.append(ids.get(i));
			if(i+1 != ids.size()){
				sb.append(",");
			}
		}
		sb.append(")");
		
		List<ForumAccount> faList = simpleJdbcTemplate.query(sb.toString(), new ForumAccountRowMapper(accList));
		
		//保证大小与顺序
		List<ForumAccount> result = new ArrayList<ForumAccount>(ids.size());
		for(int i=0; i<accList.size(); i++){
			Account acc = accList.get(i);
			if(acc == null){
				result.add(null);
				continue;
			}
			for(ForumAccount fa : faList){
				if(fa!=null && fa.getId() == acc.getId()){
					result.add(fa);
				}
			}
			if(result.size() == i){
				ForumAccount fa = new ForumAccount(acc);
				initForumAccount(fa);
				result.add(fa);
			}
		}
		return result;
	}

	@Override
	public ForumAccount loadForumAccount(Account acc) {
		// TODO Auto-generated method stub
		if(acc == null)	return null;
		ForumAccount fa = new ForumAccount(acc);
		try{
			fa = simpleJdbcTemplate.queryForObject(SQL_1, new ForumAccountRowMapper(acc), acc.getId());	
		} catch(EmptyResultDataAccessException e){
			fa = new ForumAccount(acc);
			initForumAccount(fa);
		}
		return fa;
	}

	private static class ForumAccountRowMapper implements RowMapper<ForumAccount>{
		private Account acc;
		private List<Account> accountList;
		
		private ForumAccountRowMapper(Account acc){
			this.acc = acc;
		}
		private ForumAccountRowMapper(List<Account>  accountList){
			this.accountList = accountList;
		}		
		
		@Override
		public ForumAccount mapRow(ResultSet rs, int i)
				throws SQLException {
			// TODO Auto-generated method stub
			int id = rs.getInt("acc_id");
			ForumAccount fa = null;
			if(acc == null){
				for(Account account : accountList){
					if(account != null && account.getId() == id){
						fa = new ForumAccount(account);
					}
				}
			} else {
				fa = new ForumAccount(acc);
			}
			if(fa == null)	return null;
			
			int digestCount = rs.getInt("af_digestCount");
			int replyCount = rs.getInt("af_replyCount");
			int subjectCount = rs.getInt("af_subjectCount");
			int pageView = rs.getInt("af_pageView");
			Date lastPostDate = DateInt.transform(rs.getInt("af_lastPostDate"));
			fa.setDigestCount(digestCount);
			fa.setReplyCount(replyCount);
			fa.setSubjectCount(subjectCount);
			fa.setPageView(pageView);
			fa.setLastPostDate(lastPostDate);
			return fa;
		}
		
	}
	
	private static final String SQL_2 = "insert into web_account_forum(acc_id, af_replycount, af_subjectCount, af_digestCount, af_pageView, af_lastPostDate) values(?,?,?,?,?,?)";
	//初始化web_account_forum的数据
	private void initForumAccount(ForumAccount account){
		simpleJdbcTemplate.update(SQL_2, account.getId(), account.getReplyCount(), account.getSubjectCount(), account.getDigestCount(), account.getPageView(), DateInt.transform(account.getLastPostDate()));
	}
}
